import java.util.Arrays;

public class Test {
    public static void selectSort(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left; i <= right; i++) {
                if (arr[minIndex] > arr[i]) {
                    minIndex = i;
                }
                if (arr[maxIndex] < arr[i]) {
                    maxIndex = i;
                }
            }
            swap(arr,left,minIndex);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(arr,right,maxIndex);
            left++;
            right--;
        }
    }
    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {23,122,0,12,5657,12,48,-14,2,7,2,19,24,26,10,88,66,16};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}